//
//  DKBankCardNewCell.m
//  DanKU
//
//  Created by 蓝鳍互娱 on 2023/11/28.
//

#import "DKBankCardNewCell.h"
#import "DKBaseInfoInputView.h"
#import "DKBankSelectView.h"
#import "DKBankCardPhoneAlertView.h"
static NSString * DKBankCardNewCellId = @"DKBankCardNewCellId";
@interface DKBankCardNewCell()<UIGestureRecognizerDelegate>
@property (nonatomic, strong)UIView * dk_BankCardModifyView;
@property (nonatomic, strong)UIButton * dk_NextBtn;
@property (nonatomic,strong) DKBaseInfoInputView *dk_BankAccountNo;
@property (nonatomic,strong) DKBaseInfoInputView *dk_ConfirmBankAccount;
@property (nonatomic,strong) UILabel *dk_ConfirmBankAccountAlertLab;
@property (nonatomic,strong) DKBaseInfoInputView *dk_IFSC;
@property (nonatomic,strong) DKBaseInfoInputView *dk_ConfirmIFSC;
@property (nonatomic,strong) UILabel *dk_ConfirmIFSCAlertLab;
@property (nonatomic,strong) DKBaseInfoInputView *dk_Bank;
@property (nonatomic,strong) DKBankSelectView * dk_BankSelectView;

@property (nonatomic, strong)NSArray * bankMdls;
@end
@implementation DKBankCardNewCell
+ (instancetype)bankCardNewCell:(UITableView *)tableView{
    DKBankCardNewCell * dk_BankCardNewCell = [tableView dequeueReusableCellWithIdentifier:DKBankCardNewCellId];
    if (!dk_BankCardNewCell) {
        dk_BankCardNewCell = [[DKBankCardNewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DKBankCardNewCellId];
    }
    dk_BankCardNewCell.backgroundColor = [UIColor clearColor];
    dk_BankCardNewCell.selectionStyle = UITableViewCellSelectionStyleNone;
    dk_BankCardNewCell.accessoryType = UITableViewCellAccessoryNone;
    return dk_BankCardNewCell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self dk_addSubView];
    }
    return self;
}
- (void)dk_addSubView{
    
    WeakSelf(self)
    UILabel * dk_BankCardAlertLab = [[UILabel alloc] init];
    dk_BankCardAlertLab.textColor = DK_Hex(@"#333333");
    dk_BankCardAlertLab.font = DK_FONT_Medium(17);
    dk_BankCardAlertLab.text = @"New bank card information";
    [self.contentView addSubview:dk_BankCardAlertLab];
    [dk_BankCardAlertLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(DK_IsWidthScale(76)+DK_SafeAreaTop));
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.height.equalTo(@(DK_IsWidthScale(24)));
    }];
    
    self.dk_BankCardModifyView = [[UIView alloc] init];
    self.dk_BankCardModifyView.backgroundColor = [UIColor whiteColor];
    self.dk_BankCardModifyView.layer.cornerRadius = DK_IsWidthScale(50);
    self.dk_BankCardModifyView.backgroundColor = DK_Hex(@"#FFFFFF");
    [self.contentView addSubview:self.dk_BankCardModifyView];
    [self.dk_BankCardModifyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(DK_IsWidthScale(120)+DK_SafeAreaTop));
        make.left.equalTo(@(DK_IsWidthScale(0)));
        make.right.equalTo(@(-DK_IsWidthScale(0)));
        make.bottom.equalTo(@(DK_IsWidthScale(0)));
    }];
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddBankSelect)];
    tapGes.delegate = self;
    [self.dk_BankCardModifyView addGestureRecognizer:tapGes];
    
    UIView * dk_PanCardBottomView = [[UIView alloc] init];
    dk_PanCardBottomView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:dk_PanCardBottomView];
    [dk_PanCardBottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(DK_IsWidthScale(0)));
        make.height.equalTo(@(DK_IsWidthScale(50)));
        make.top.equalTo(self.dk_BankCardModifyView.mas_top);
    }];
    
    self.dk_BankAccountNo = [[DKBaseInfoInputView alloc] init];
    self.dk_BankAccountNo.inputStatus = @"2";
    self.dk_BankAccountNo.authBaseStepInputType = AuthBaseStepInputTypeBankAccountNo;
    [self.dk_BankCardModifyView addSubview:self.dk_BankAccountNo];
    [self.dk_BankAccountNo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(@(DK_IsWidthScale(20)));
        make.height.equalTo(@(DK_IsWidthScale(90)));
    }];
    self.dk_BankAccountNo.InputScuessBlock = ^{
        [weakSelf makeNextStatus];
    };
    
    self.dk_ConfirmBankAccount = [[DKBaseInfoInputView alloc] init];
    self.dk_ConfirmBankAccount.inputStatus = @"2";
    self.dk_ConfirmBankAccount.authBaseStepInputType = AuthBaseStepInputTypeConfirmBankAccount;
    [self.dk_BankCardModifyView addSubview:self.dk_ConfirmBankAccount];
    [self.dk_ConfirmBankAccount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(self.dk_BankAccountNo.mas_bottom);
        make.height.equalTo(@(DK_IsWidthScale(90)));
    }];
    self.dk_ConfirmBankAccount.InputScuessBlock = ^{
        [weakSelf makeNextStatus];
    };
    self.dk_ConfirmBankAccountAlertLab = [[UILabel alloc] init];
    self.dk_ConfirmBankAccountAlertLab.textColor = DK_Hex(@"#ED4342");
    self.dk_ConfirmBankAccountAlertLab.font = DK_FONT_Regular(9.5);
    self.dk_ConfirmBankAccountAlertLab.text = @"Please make sure that the account number entered twice is the same";
    self.dk_ConfirmBankAccountAlertLab.numberOfLines = 0;
    self.dk_ConfirmBankAccountAlertLab.hidden = YES;
    [self.dk_BankCardModifyView addSubview:self.dk_ConfirmBankAccountAlertLab];
    [self.dk_ConfirmBankAccountAlertLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(self.dk_ConfirmBankAccount.mas_bottom);
        make.height.equalTo(@(DK_IsWidthScale(0)));
    }];
    
    self.dk_IFSC = [[DKBaseInfoInputView alloc] init];
    self.dk_IFSC.inputStatus = @"2";
    self.dk_IFSC.authBaseStepInputType = AuthBaseStepInputTypeIFSC;
    [self.dk_BankCardModifyView addSubview:self.dk_IFSC];
    [self.dk_IFSC mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(self.dk_ConfirmBankAccountAlertLab.mas_bottom).offset(DK_IsWidthScale(0));
        make.height.equalTo(@(DK_IsWidthScale(90)));
    }];
    self.dk_IFSC.InputScuessBlock = ^{
        [weakSelf makeNextStatus];
    };
    
    self.dk_ConfirmIFSC = [[DKBaseInfoInputView alloc] init];
    self.dk_ConfirmIFSC.inputStatus = @"2";
    self.dk_ConfirmIFSC.authBaseStepInputType = AuthBaseStepInputTypeConfirmIFSC;
    [self.dk_BankCardModifyView addSubview:self.dk_ConfirmIFSC];
    [self.dk_ConfirmIFSC mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(self.dk_IFSC.mas_bottom);
        make.height.equalTo(@(DK_IsWidthScale(90)));
    }];
    self.dk_ConfirmIFSC.InputScuessBlock = ^{
        [weakSelf makeNextStatus];
    };
    
    self.dk_ConfirmIFSCAlertLab = [[UILabel alloc] init];
    self.dk_ConfirmIFSCAlertLab.textColor = DK_Hex(@"#ED4342");
    self.dk_ConfirmIFSCAlertLab.font = DK_FONT_Regular(10);
    self.dk_ConfirmIFSCAlertLab.text = @"Please make sure the IFSC is the same for both inputs";
    self.dk_ConfirmIFSCAlertLab.hidden = YES;
    self.dk_ConfirmIFSCAlertLab.numberOfLines = 0;
    [self.dk_BankCardModifyView addSubview:self.dk_ConfirmIFSCAlertLab];
    [self.dk_ConfirmIFSCAlertLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(self.dk_ConfirmIFSC.mas_bottom).offset(DK_IsWidthScale(0));
        make.height.equalTo(@(DK_IsWidthScale(0)));
    }];
    
    self.dk_Bank = [[DKBaseInfoInputView alloc] init];
    self.dk_Bank.inputStatus = @"2";
    self.dk_Bank.authBaseStepInputType = AuthBaseStepInputTypeBank;
    [self.dk_BankCardModifyView addSubview:self.dk_Bank];
    [self.dk_Bank mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(self.dk_ConfirmIFSCAlertLab.mas_bottom).offset(DK_IsWidthScale(0));
        make.height.equalTo(@(DK_IsWidthScale(90)));
    }];
    self.dk_Bank.InputScuessBlock = ^{
        [weakSelf makeNextStatus];
        [weakSelf getSourchBank];
    };
    //next
    self.dk_NextBtn = [[UIButton alloc] init];
    [self.dk_NextBtn setTitle:@"SUBMIT" forState:UIControlStateNormal];
    [self.dk_NextBtn setTitleColor:DK_Hex(@"#4566FF") forState:UIControlStateDisabled];
    [self.dk_NextBtn setTitleColor:DK_Hex(@"#FFFFFF") forState:UIControlStateNormal];
    self.dk_NextBtn.titleLabel.font = DK_FONT_Semibold(16);
    [self.dk_NextBtn setBackgroundImage:DK_GetImage(@"dk_registerNextSelectBg") forState:UIControlStateNormal];
    [self.dk_NextBtn setBackgroundImage:DK_GetImage(@"dk_registerNextBg") forState:UIControlStateDisabled];
    self.dk_NextBtn.enabled = NO;
    [self.dk_NextBtn addTarget:self action:@selector(goCheck) forControlEvents:UIControlEventTouchUpInside];
    [self.dk_BankCardModifyView addSubview:self.dk_NextBtn];
    [self.dk_NextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.height.equalTo(@(DK_IsWidthScale(50)));
        make.top.equalTo(self.dk_Bank.mas_bottom).offset(DK_IsWidthScale(20));
    }];
    UIButton * dk_AgreementBtn = [[UIButton alloc] init];
    [dk_AgreementBtn setTitle:@"《Privacy Agreement》" forState:UIControlStateNormal];
    [dk_AgreementBtn setTitleColor:DK_Hex(@"#CCCCCC") forState:UIControlStateNormal];
    dk_AgreementBtn.titleLabel.font = DK_FONT_Regular(12);
    [dk_AgreementBtn addTarget:self action:@selector(CheckAgreement) forControlEvents:UIControlEventTouchUpInside];
    [self.dk_BankCardModifyView addSubview:dk_AgreementBtn];
    [dk_AgreementBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self.dk_NextBtn.mas_bottom).offset(DK_IsWidthScale(20));
        make.height.equalTo(@(DK_IsWidthScale(17)));
    }];
    
    self.dk_BankSelectView = [[DKBankSelectView alloc] init];
    self.dk_BankSelectView.hidden = YES;
    [self.dk_BankCardModifyView addSubview:self.dk_BankSelectView];
    [self.dk_BankSelectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.bottom.equalTo(self.dk_Bank.mas_top).offset(DK_IsWidthScale(20));
        make.height.equalTo(@(DK_IsWidthScale(0)));
    }];
    self.dk_BankSelectView.BankSelectScuessBlock = ^(NSString * _Nonnull bankName) {
        weakSelf.dk_BankSelectView.hidden = YES;
        weakSelf.dk_Bank.bankInfo = bankName;
        [weakSelf makeNextStatus];
    };
    [self requestBankList];
}
//-(void)layoutSubviews{
//    
//}
//-(void)layoutSublayersOfLayer:(CALayer *)layer{
//    [super layoutSublayersOfLayer:layer];
//    [self.dk_BankCardModifyView makeCornerOnTopleft:DK_IsWidthScale(50)];
//}
- (void)hiddBankSelect{
    self.dk_BankSelectView.hidden = YES;
}
- (void)makeNextStatus{
    if(([self.dk_BankAccountNo getStepStatusInput].length > 0)&&([self.dk_ConfirmBankAccount getStepStatusInput].length > 0)&&([self.dk_IFSC getStepStatusInput].length > 0)&&([self.dk_ConfirmIFSC getStepStatusInput].length > 0)&&([self.dk_Bank getStepStatusInput].length > 0)){
        self.dk_NextBtn.enabled = YES;
    }else{
        self.dk_NextBtn.enabled = NO;
    }
    BOOL isErrorBankAccount = NO;
    BOOL isErrorIFSC = NO;
    if(![[self.dk_BankAccountNo getStepStatusInput] isEqualToString:[self.dk_ConfirmBankAccount getStepStatusInput]]&&[self.dk_BankAccountNo getStepStatusInput].length&&[self.dk_ConfirmBankAccount getStepStatusInput].length){
        isErrorBankAccount = YES;
        self.dk_ConfirmBankAccountAlertLab.hidden = NO;
        [self.dk_ConfirmBankAccountAlertLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(DK_IsWidthScale(14)));
        }];
        [self.dk_ConfirmBankAccount changeErrorStepStatusInput:YES];
        [self.dk_IFSC mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.dk_ConfirmBankAccountAlertLab.mas_bottom).offset(DK_IsWidthScale(20));
        }];
    }else{
        self.dk_ConfirmBankAccountAlertLab.hidden = YES;
        [self.dk_ConfirmBankAccountAlertLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(DK_IsWidthScale(0)));
        }];
        [self.dk_ConfirmBankAccount changeErrorStepStatusInput:NO];

        [self.dk_IFSC mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.dk_ConfirmBankAccountAlertLab.mas_bottom).offset(DK_IsWidthScale(0));
        }];
    }
    if(![[self.dk_IFSC getStepStatusInput] isEqualToString:[self.dk_ConfirmIFSC getStepStatusInput]]&&[self.dk_IFSC getStepStatusInput].length&&[self.dk_ConfirmIFSC getStepStatusInput].length){
        isErrorIFSC = YES;
        self.dk_ConfirmIFSCAlertLab.hidden = NO;
        [self.dk_ConfirmIFSCAlertLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(DK_IsWidthScale(14)));
        }];
        [self.dk_ConfirmIFSC changeErrorStepStatusInput:YES];
        [self.dk_Bank mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.dk_ConfirmIFSCAlertLab.mas_bottom).offset(DK_IsWidthScale(20));
        }];
    }else{
        self.dk_ConfirmIFSCAlertLab.hidden = YES;
        [self.dk_ConfirmIFSCAlertLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.dk_ConfirmIFSC.mas_bottom).offset(DK_IsWidthScale(0));
            make.height.equalTo(@(DK_IsWidthScale(0)));
        }];
        [self.dk_ConfirmIFSC changeErrorStepStatusInput:NO];
        [self.dk_Bank mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.dk_ConfirmIFSCAlertLab.mas_bottom).offset(DK_IsWidthScale(0));
        }];
    }
//    if(self.CheckErrorBlock){
//        self.CheckErrorBlock(isErrorBankAccount, isErrorIFSC);
//    }
}
- (void)requestBankList{
    WeakSelf(self)
    [DKPublicHttp apiGetBankListForAPI:@{} complete:^(NSArray * _Nonnull bankMdls) {
        weakSelf.bankMdls = bankMdls;
    }];
}
- (void)getSourchBank{
    NSMutableArray *sourchBanks = [[NSMutableArray alloc] init];
    NSString *inputBankName = [self.dk_Bank getStepStatusInput];
    for (NSString *sourchBank in self.bankMdls) {
        if([sourchBank rangeOfString:inputBankName options:NSCaseInsensitiveSearch].location != NSNotFound){
            [sourchBanks insertObject:sourchBank atIndex:0];
        }
    }
    if(sourchBanks.count > 0){
        self.dk_BankSelectView.hidden = NO;
        if(sourchBanks.count > 3){
            [self.dk_BankSelectView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@(DK_IsWidthScale(120)));
            }];
        }else{
            [self.dk_BankSelectView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@(DK_IsWidthScale(40)*sourchBanks.count));
            }];
        }
    }else{
        self.dk_BankSelectView.hidden = YES;
        [self.dk_BankSelectView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(DK_IsWidthScale(0)));
        }];
    }
    self.dk_BankSelectView.bankSelects = sourchBanks.copy;
}
- (void)goCheck{
    WeakSelf(self)
    [DKPublicHttp apiUserBrhavRecordForAPI:@{} withPageName:DK_EditBankcard withAction:DK_ActionClick complete:^{
            
    } fail:^{
            
    }];
    if([[self.dk_BankAccountNo getStepStatusInput] isEqualToString:[self.dk_ConfirmBankAccount getStepStatusInput]]&&[[self.dk_IFSC getStepStatusInput] isEqualToString:[self.dk_ConfirmIFSC getStepStatusInput]]){
        NSMutableDictionary *userInfoDataDic = [[NSMutableDictionary alloc] init];
        [userInfoDataDic setObject:[self.dk_BankAccountNo getStepStatusInput] forKey:@"bankCard"];
        [userInfoDataDic setObject:[self.dk_IFSC getStepStatusInput] forKey:@"bankCode"];
        [userInfoDataDic setObject:[self.dk_Bank getStepStatusInput] forKey:@"bankName"];
        [DKBankCardPhoneAlertView dk_ShowBankCardPhoneAlertView:userInfoDataDic withView:self.bankCardView complete:^{
            if(weakSelf.RequestScuessBankCardAuthBlock){
                weakSelf.RequestScuessBankCardAuthBlock();
            }
            [DKPublicHttp apiUserBrhavRecordForAPI:@{} withPageName:DK_EditBankcard withAction:DK_ActionSave complete:^{
                    
            } fail:^{
                    
            }];
        }];
    }
}
- (void)CheckAgreement{
    if(self.CheckPrivateAgrmentBlock){
        self.CheckPrivateAgrmentBlock();
    }
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if ([NSStringFromClass([touch.view class])isEqualToString:@"CollctionCell"] ) {
        return NO;
    }
    if([NSStringFromClass([touch.view class])isEqual:@"UITableViewCellContentView"]){
        return NO;//当触碰到TableView的Cell的ContentView手势事件不响应
    }

    return YES;
}
@end
